package App.Controller;

import App.Model.Project.*;
import App.Model.Task.GetTaskPageItemsRequest;
import App.Model.Task.ResultResponse;
import App.Repository.ProjectRepository;
import App.Service.ProjectService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;


import App.Model.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by xw on 2018/11/20.
 */
@RestController
@RequestMapping("/api/project/")
@Api(tags = "项目模块")
public class ProjectController extends BaseController {

    @Autowired
    private ProjectService _service;

    @RequestMapping("list")
    public ResponseEntity<?> getProjectList() {
        DataResponse _dataResponse = new DataResponse();
        try {
            ProjectGetListRequest _request = new ProjectGetListRequest();
            _request.setSearchkey("");

            List<ProjectList> _result = _service.getProjectlist(_request, userId(), "project");
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    @RequestMapping("v2/list")
    public ResponseEntity<?> getProjectListV2(@RequestBody(required = false) ProjectGetListRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            if (_request == null) {
                _request = new ProjectGetListRequest();
            }
            if (!StringUtils.hasText(_request.getSearchkey())) {
                _request.setSearchkey("");
            }
            if (_request.getPageindex() == 0) {
                _request.setPageindex(1);
            }
            if (_request.getPagesize() == 0) {
                _request.setPagesize(20);
            }
            List<ProjectList> _result = _service.getProjectlist(_request, userId(), "project");
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //保存，编辑项目
    @RequestMapping("save")
    @ApiOperation("保存项目")
    public ResponseEntity<BaseResponse> saveProject(@RequestBody(required = false) ProjectSaveRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        try {

            Map<String, Object> result = _service.saveProject(_request, userId());
            _baseResponse = ResponseError(null, "参数格式错误");
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            int count = (int) result.get("result");
            if (count > 0) {
                _baseResponse.setData(result.get("id"));
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }

        } catch (Exception ex) {
            _baseResponse.setData(ex.getMessage());
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    @RequestMapping("detail")
    public ResponseEntity<?> getProjectDetail(@RequestHeader(value = "device", required = false) String device,
            @RequestBody(required = false) ProjectDetailRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            ProjectDetailResponse _detailResponse = _service.detail(_request.getProjectid(), userId(),device);
            _dataResponse.setData(_detailResponse);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    @RequestMapping("name")
    public String showName() {
        int userid = userId();
        return Integer.toString(userid);
    }

    //项目阶段
    @RequestMapping("stages")
    public ResponseEntity<?> getStage(@RequestBody(required = false) ProjectStageGetRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {

            List<ProjectStageListResponse> _results = new ArrayList<>();
            List<CrmProjectStage> _stages = _service.getProjectStages(_request);
            for (CrmProjectStage s : _stages) {

                ProjectStageListResponse _r = new ProjectStageListResponse();
                _r.setStagename(s.getStagename());
                _r.setStageid(s.getStageid());
                _r.setDatastageid(s.getDatastageid());
                _r.setOrder(s.getOrder());

                if (s.getStagetype() == BaseEnum.StageType.Define.getStageType()) {
                    _r.setIsuserdefine(true);
                } else {
                    _r.setIsuserdefine(false);
                }
                _results.add(_r);
            }
            _dataResponse.setData(_results);
        } catch (Exception ex) {
            ex.printStackTrace();
            _dataResponse.setData(ex.getMessage());
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //法庭
    @RequestMapping("courts")
    public ResponseEntity<?> getCourts(@RequestBody(required = false) ProjectCourtGetRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<CrmProjectCourt> _stages = _service.getProjectCourts(_request);
            _dataResponse.setData(_stages);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //案由
    @RequestMapping("causeofaction")
    public ResponseEntity<?> getCauseofaction(@RequestBody(required = false) ProjectCaseGetRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectCause> _stages = _service.getProjectCause(_request);
            _dataResponse.setData(_stages);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //获取项目文件夹
    @RequestMapping("folder")
    public ResponseEntity<?> getPorjectFolder(@RequestBody(required = false) ProjectFolderGetRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        String _requestString = JSONObject.toJSONString(_request);
        Log log = LogFactory.getLog(ProjectRepository.class);
        log.info("get folders request:" + _requestString);

        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectFolderResponse> _folders = _service.getProjectFolder(_request, userId());
            _dataResponse.setData(_folders);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    @RequestMapping("member")
    public ResponseEntity<?> getChatMember(@RequestBody(required = false) ProjectMemberGetRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {

            List<ProjectMember> _result = _service.getProjectMember(_request);
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //添加聊天组成员
    @RequestMapping("addmember")
    public ResponseEntity<?> addMember(@RequestBody(required = false) ProjectMemberAddRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        _baseResponse.setMessage("操作失败");
        _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.addMember(_request, userId());
            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            } else if (_result == -1) {
                _baseResponse.setMessage("没有删除成员权限");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setData(ex.getMessage());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //删除聊天成员
    @RequestMapping("deletemember")
    public ResponseEntity<?> deleteMember(@RequestBody(required = false) ProjectMemberDeleteRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        _baseResponse.setMessage("操作失败");
        _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            ResultResponse _result = _service.deleteMember(_request, userId());
            if (_result.getResultcode() > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
                return new ResponseEntity(_baseResponse, HttpStatus.OK);
            } else {
                _baseResponse.setMessage("操作失败");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
                return new ResponseEntity(_baseResponse, HttpStatus.OK);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    @RequestMapping("deletestage")
    public ResponseEntity<?> deleteProjectStage(@RequestBody(required = false) ProjectStageDeleteRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.deleteStage(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //项目类型
    @RequestMapping("types")
    public ResponseEntity<?> getPorjectTypes() {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<CrmProjectType> _stages = _service.getProjectTypes();
            _dataResponse.setData(_stages);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //完成阶段
    @RequestMapping("completestage")
    public ResponseEntity<?> completeProjectStage(@RequestBody(required = false) ProjectStageStatusUpdateRequest _request) throws Exception {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.completeStage(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    @RequestMapping("restartstage")
    public ResponseEntity<?> restartProjectStage(@RequestBody(required = false) ProjectStageStatusUpdateRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.restartStage(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //获取项目委托人和对方当事人的身份
    @RequestMapping("titles")
    public ResponseEntity<?> getProjectTitle(@RequestBody(required = false) ProjectTitleGetRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectTitle> _result = _service.getProjectTitle(_request);
            _dataResponse.setData(_result);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //保存，编辑项目
    @RequestMapping("addtitle")
    public ResponseEntity<BaseResponse> saveProjectTitle(@RequestBody(required = false) ProjectSaveTitleRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");


        System.out.println("step 1");
        System.out.println("projecttype:" + _request.getProjecttype());
        System.out.println("stageid:" + _request.getStageid());
        System.out.println("title:" + _request.getTitle());

        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        System.out.println("step 2");
        try {

            System.out.println("step 3");
            int result = _service.saveProjectTitle(_request, userId());

            System.out.println("step 4");
            _baseResponse = ResponseError(null, "参数格式错误");
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            if (result > 0) {
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setData(ex.getMessage());
        }

        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //获取项目信息可以显示的字段
    @RequestMapping("fields")
    public ResponseEntity<?> getProjectInfoFields(@RequestBody(required = false) ProjectInfoFieldGetRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectInfoField> _result = _service.getProjectInfoFields(_request, userId());
            _dataResponse.setData(_result);

        } catch (Exception ex) {
            ex.printStackTrace();
            _dataResponse.setData(ex.getMessage());
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //项目的文件夹为任务转移文件到项目使用
    @RequestMapping("folderlist")
    public ResponseEntity<?> getPorjectFolderList(@RequestBody(required = false) ProjectTaskFolderGetRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectFolderResponse> _folders = _service.getProjectTaskFolder(_request, userId());
            _dataResponse.setData(_folders);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    @RequestMapping("projectstages")
    public ResponseEntity<?> getProjectStages() {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectList> _result = _service.getProjectListForTask(userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    @RequestMapping("savestage")
    public ResponseEntity<BaseResponse> saveProjectStage(@RequestBody(required = false) ProjectStageSaveRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        try {
            ProjectSaveStageResponse _tmpResult = _service.saveProjectStage(_request, userId());
            int _result = _tmpResult.getResult();
            if (_result < 1) {
                if (_result == -1) {
                    _baseResponse.setMessage("阶段名称已经存在");
                } else {
                    _baseResponse.setMessage("保存失败");
                }
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            } else {
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
                _baseResponse.setData(_tmpResult);
            }

        } catch (Exception ex) {
            _baseResponse.setData(ex.getMessage());
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //自定义文件夹
    @RequestMapping("savefolder")
    public ResponseEntity<BaseResponse> saveProjecFolder(@RequestBody(required = false) ProjectSaveFolderRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            ProjectSaveFolderResponse result = _service.saveProjectDefineFolder(_request, userId());
            _baseResponse = ResponseError(null, "参数格式错误");
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (result != null && !result.getFolderid().isEmpty()) {
                _baseResponse.setData(result);
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            } else {
                _baseResponse.setData(result);
                _baseResponse.setMessage("文件夹已经存在");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setData(ex.getMessage());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //获取所项目阶段和相关文件夹,为复制文件，选择文件夹使用
    @RequestMapping("projectfolders")
    public ResponseEntity<?> getAllPorjectFolders(@RequestBody(required = false) ProjectGetAllFolderRequest _request) {

        DataResponse _dataResponse = new DataResponse();
        try {
            List<GetAllProjectFolderResponse> _result = _service.getAllPorjectFolders(_request, userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //复制文件到某个项目的阶段的文件夹
    @RequestMapping("filemerge")
    public ResponseEntity<BaseResponse> projecFileMerge(@RequestBody(required = false) ProjectFileMergeRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int result = _service.projecFileMerge(_request, userId());
            _baseResponse = ResponseError(null, "参数格式错误");
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (result > 0) {
                _baseResponse.setData(result);
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setData(ex.getMessage());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //完成项目
    @RequestMapping("complete")
    public ResponseEntity<?> CompleteProject(@RequestBody(required = false) ProjectCompleteRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.projectStatusChange(_request, BaseEnum.ProjectStatusType.Complete.getProjectStatusType(), userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //重启项目
    @RequestMapping("restart")
    public ResponseEntity<?> RestartProject(@RequestBody(required = false) ProjectCompleteRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.projectStatusChange(_request, BaseEnum.ProjectStatusType.UnComplete.getProjectStatusType(), userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //删除项目
    @RequestMapping("delete")
    public ResponseEntity<?> DeleteProject(@RequestBody(required = false) ProjectCompleteRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.deleteProject(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //任务关联项目时候的选择列表
    @RequestMapping("listfortask")
    public ResponseEntity<?> getProjectListForTask(@RequestBody(required = false) ProjectGetListRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectList> _result = _service.getProjectlist(_request, userId(), "task");
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    @RequestMapping("allfields")
    public ResponseEntity<?> getProjectInfoAllFields(@RequestBody(required = false) ProjectAllFieldRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectInfoAllFieldResponse> _result = _service.getProjectInfoAllFields(_request, userId());
            _dataResponse.setData(_result);

        } catch (Exception ex) {
            ex.printStackTrace();
            _dataResponse.setData(ex.getMessage());
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //保存项目名称
    @RequestMapping("savename")
    public ResponseEntity<?> saveProjectName(@RequestBody(required = false) ProjectSaveNameRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.saveProjectName(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //保存项目信息
    @RequestMapping("saveinfo")
    public ResponseEntity<?> saveProjectInfo(@RequestBody(required = false) ProjectSaveInfoRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.saveProjectInfo(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //保存项目单个阶段的全部数据
    @RequestMapping("savestagedata")
    public ResponseEntity<?> saveProjectStageData(@RequestBody(required = false) ProjectSaveStageRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.saveProjectStageData(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    /***
     * 重命名项目文件
     * @param _request
     * @return
     */
    @RequestMapping("renamefile")
    public ResponseEntity<?> renameProjectFile(@RequestBody(required = false) ProjectRenameFileRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.renameProjectFile(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    /**
     * 删除项目文件
     *
     * @param _request
     * @return
     */
    @RequestMapping("deletefile")
    public ResponseEntity<?> deleteProjectFile(@RequestBody(required = false) ProjectDeleteFileRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.deleteProjectFile(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    @RequestMapping("deletefiles")
    public ResponseEntity<?> deleteProjectFiles(@RequestBody(required = false) ProjectDeleteFileRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            List<String> ids = _request.getFilerecids();
            int _result = 0;
            if (ids != null && ids.size() > 0) {
                ProjectDeleteFileRequest req = new ProjectDeleteFileRequest();
                req.setProjectid(_request.getProjectid());
                req.setStageid(_request.getStageid());
                for (String str : ids) {
                    req.setFilerecid(str);
                    _result = _service.deleteProjectFile(req, userId());
                }
            }
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    /**
     * 保存项目文件
     *
     * @param _request
     * @return
     */
    @RequestMapping("addfile")
    public ResponseEntity<?> addProjectFile(@RequestBody(required = false) ProjectAddFileRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.addProjectFile(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    /**
     * 保存项目文件
     *
     * @param _request
     * @return
     */
    @RequestMapping("v2/addfile")
    public ResponseEntity<?> addProjectFile2(@RequestBody(required = false) ProjectAddFileRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.addProjectFile2(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    /**
     * 设置为主办人
     *
     * @param _request
     * @return
     */
    @RequestMapping("sethost")
    public ResponseEntity<?> setProjectHost(@RequestBody(required = false) ProjectSetHostRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.setProjectHost(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    /*
     * 设置为管理员
     * */
    @RequestMapping("setmanager")
    public ResponseEntity<?> setProjectManager(@RequestBody(required = false) ProjectSetManagerRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.setProjectManager(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //获取task下的文件
    @RequestMapping("getfiles")
    public ResponseEntity<?> getProjectFiles(@RequestBody(required = false) GetProjectFileRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<ProjectFileFolder> _result = _service.getProjectFiles(_request, userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //获取分页数据
    @RequestMapping("page")
    public ResponseEntity<?> getProjectPageItems(@RequestBody(required = false) GetTaskPageItemsRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {

            GetProjectPageItemsResponse _result = _service.getProjectPageItems(_request, userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //获取未完成的项目
    @RequestMapping("uncompleted")
    public ResponseEntity<?> getUncompletedProject(@RequestBody(required = false) ProjectGetListRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            ProjectGetListRequest _request2 = new ProjectGetListRequest();
            _request2.setSearchkey(_request.getSearchkey());

            List<ProjectList> _result = _service.getUnCompletedProjecs(_request2, userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    /**
     * 设置文件为已经阅读
     *
     * @param _request
     * @return
     */
    @RequestMapping("viewfile")
    public ResponseEntity<?> setFileViewed(@RequestBody(required = false) ProjectViewdFileRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.setFileViewed(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    /**
     * set folder status
     */
    @RequestMapping("foldervisible")
    public ResponseEntity<?> setFolderLawyerVisible(@RequestBody(required = false) ProjectSetFolderVisibleRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.setFolderLawyerVisible(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


//project permission

    @RequestMapping("permission")
    public ResponseEntity<?> getProjectPermission(@RequestBody(required = false) GetPermissionRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {

            GetPermissionResponse _result = _service.getProjectPermission(_request, userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    @RequestMapping("exportprojectstagefile")
    public ResponseEntity<?> exportProjectStageFile(@RequestBody(required = false) ProjectStageStatusUpdateRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            Map<String, Object> _result = _service.exportProjectStageFile(_request, userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //end of class

}
